import React, { useState,useEffect } from 'react';
import { async_add,async_edit } from '../../store/crimsSlice';
import {useDispatch,useSelector} from "react-redux"
import {
  Button,
  Cascader,
  DatePicker,
  Form,
  Input,
  InputNumber,
  Radio,
  Select,
  Switch,
  TreeSelect,
} from 'antd';
const Myfrom = (props) => {
    let obj=useSelector(state=>state.todos)
    let dispatch=useDispatch()
     const [form] = Form.useForm();
    useEffect(()=>{
        form.setFieldsValue(props.itemObj)
    })
    const onFinish = values => {
        if(props.title=='添加'){
            let obj={
    "id": new Date().getTime(),
    ...values
            }
            dispatch(async_add(obj))
      
        }else{
        let obj={
    "id": props.itemObj.id,
    ...values
            }
            dispatch(async_edit(obj))
        }
      props.onCancel()
  console.log('Success:', values);
};

  return (
   <Form
onFinish={onFinish}
     form={form}
    >
 
      <Form.Item label="岗位编号" name='code'>
        <Input />
      </Form.Item>
      <Form.Item label="上级部门" name="name"
        rules={[{ required: true, message: '请选择上级部门' }]}
      >
        <Select>
          <Select.Option value="ceo">ceo</Select.Option>
          <Select.Option value="se">se</Select.Option>
          <Select.Option value="hr">hr</Select.Option>
          <Select.Option value="user">user</Select.Option>
        </Select>
      </Form.Item>
    <Form.Item label="部门名称" name='titles'
         rules={
            
            [
                  { min:4,max:10,required: true, message: '4-10个字符' },
                { required: true, message: '请输入部门名称' }


         ]}
    >
        <Input />
      </Form.Item>
    
      <Form.Item label="排序" name='sort'
              rules={[{ required: true, message: '请输入数字' }]}
      >
        <InputNumber />
      </Form.Item>
      <Form.Item label="状态" name='state'>
       <Radio.Group

    options={[
      { value: true, label: '正常' },
      { value: false, label: '异常' },
     
    ]}
  />
      </Form.Item>
      <Form.Item >
        <Button htmlType="submit" type="primary">完成</Button>
      </Form.Item>
    </Form>
  );
};
export default Myfrom;